home *** CD-ROM | disk | FTP | other *** search
- unit CopyAFileForm;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- btnCopy: TButton;
- dlgSource: TOpenDialog;
- dlgTarget: TSaveDialog;
- procedure btnCopyClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- {$ifdef Ver90} //Delphi 2.0x doesn't have Win32Check
- type
- EWin32Error = class(Exception);
-
- procedure Win32Check(RetVal: Bool);
- begin
- if not RetVal then
- raise EWin32Error.Create(SysErrorMessage(GetLastError));
- end;
- {$endif}
-
- procedure FileCopy(const Source, Target: String);
- var
- LastError: DWord;
- begin
- if not CopyFile(PChar(Source), PChar(Target), True) then
- begin
- LastError := GetLastError;
- //If file exists, check it's okay to overwrite
- if LastError = ERROR_FILE_EXISTS then
- begin
- if MessageDlg('Destination file exists. Overwrite?',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes then
- //If it's okay, then overwrite
- Win32Check(CopyFile(PChar(Source), PChar(Target), False))
- end
- else
- //If there was some other problem, report it
- raise EWin32Error.Create(SysErrorMessage(LastError));
- end
- end;
-
- procedure TForm1.btnCopyClick(Sender: TObject);
- begin
- if dlgSource.Execute and dlgTarget.Execute then
- FileCopy(dlgSource.FileName, dlgTarget.FileName)
- end;
-
- end.
-